2
2
.
.
7
7
.
.
1
1
@
@
O
O
n
n
e
e
T
T
o
o
O
O
n
n
e
e
-
-
J
J
o
o
i
i
n
n
T
T
a
a
b
b
l
l
e
e
I
I
n
n
f
f
o
o
[
[
G
G
]
]
[
[
R
R
]
]
This tutorial shows how to implement @OneToOne relationship by using Join Table.
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables @Controller and @RequestMapping. Includes Tomcat Server.
SQL
Spring Data JPA
Enables @Entity and @Id
SQL
H2 Database
Enables in-memory H2 Database
DB Schema
Syntax
@OneToOne(cascade = CascadeType.ALL)
@JoinTable(name = "AUTHOR_ADDRESS",
joinColumns = { @JoinColumn(name = "AUTHOR_ID" , referencedColumnName = "id") },
inverseJoinColumns = { @JoinColumn(name = "ADDRESS_ID", referencedColumnName = "id") }
)
public Address address;
ADDRESS
CITY
STREET
ID
NAME
AGE
ID
ADDRESS_ID
AUTHOR_ADDRESS
ID
MyController
http://localhost:8080/AddAuthor
Author
AuthorRepository
addAuthor()
Address
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_relationships_onetoone_jointable (add Spring Boot Starters from the table)
Edit FIle: application.properties (specify H2 DB name & enable H2 Web Console)
Create Package: entities (inside main package)
– Create Class: Author.java (inside package entities)
– Create Class: Address.java (inside package entities)
Create Package: repositories (inside main package)
– Create Interface: AuthorRepository.java (inside package repositories)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside package controllers)
application.properties
spring.datasource.url = jdbc:h2:mem:testdb
spring.h2.console.enabled = true
Author.java
package com.ivoronline.springboot_relationships_onetoone_jointable.entities;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.JoinTable;
import javax.persistence.OneToOne;
import javax.persistence.CascadeType;
@Entity
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer id;
public String name;
public Integer age;
@OneToOne(cascade = CascadeType.ALL)
@JoinTable(name = "AUTHOR_ADDRESS")
public Address address;
}
Address.java
package com.ivoronline.springboot_relationships_onetoone_jointable.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer id;
public String city;
public String street;
}
AuthorRepository.java
package com.ivoronline.springboot_relationships_onetoone_jointable.repositories;
import com.ivoronline.springboot_relationships_onetoone_jointable.entities.Author;
import org.springframework.data.repository.CrudRepository;
public interface AuthorRepository extends CrudRepository<Author, Integer> { }
MyController.java
package com.ivoronline.springboot_relationships_onetoone_jointable.controllers;
import com.ivoronline.springboot_relationships_onetoone_jointable.entities.Author;
import com.ivoronline.springboot_relationships_onetoone_jointable.entities.Address;
import com.ivoronline.springboot_relationships_onetoone_jointable.repositories.AuthorRepository;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController {
@Autowired
AuthorRepository authorRepository;
@ResponseBody
@RequestMapping("/AddAuthor")
public String addAuthor() {
//CREATE ADDRESS ENTITY
Address address = new Address();
address.city = "London";
address.street = "Piccadilly";
//CREATE AUTHOR ENTITY
Author author = new Author();
author.name = "John";
author.age = 20;
author.address = address;
//STORE AUTHOR/ADDRESS ENTITY INTO DB
authorRepository.save(author);
//RETURN SOMETHING TO BROWSER
return "Author/Address was stored into DB";
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/AddAuthor
AUTHOR ADDRESS (http://localhost:8080/h2-console)
AUTHOR_ADDRESS
Application Structure
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>